<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface{

	protected $table = 'users';
	
	protected $hidden = array('password');
	
	public static $rules=array(
			'username' => 'required',
			'email' => 'required|email',
			'password'=>'sometimes|required|min:8',
			'roles' => 'sometimes|required'
	);
	protected $fillable = array('username', 'email','password');
	/**
	 * Get the unique identifier for the user.
	 *
	 * @return mixed
	 */
	public function getAuthIdentifier()
	{
		return $this->getKey();
	}
	
	/**
	 * Get the password for the user.
	 *
	 * @return string
	 */
	public function getAuthPassword()
	{
		return $this->password;
	}
	
	/**
	 * Get the token value for the "remember me" session.
	 *
	 * @return string
	 */
	public function getRememberToken()
	{
		return $this->remember_token;
	}
	
	/**
	 * Set the token value for the "remember me" session.
	 *
	 * @param  string  $value
	 * @return void
	 */
	public function setRememberToken($value)
	{
		$this->remember_token = $value;
	}
	
	/**
	 * Get the column name for the "remember me" token.
	 *
	 * @return string
	 */
	public function getRememberTokenName()
	{
		return 'remember_token';
	}
	
	public function roles(){
		return $this->belongsToMany('Rol','usuarios_roles');
	}
	
	public function noticias()
	{
		return $this->hasMany('Noticia');
	}
	
	public function comentarios(){
		return $this->hasMany('Comentario');
	}
	
	public function autortorneos(){
		return $this->hasMany('Torneo');
	}
	
	public function torneos(){
		return $this->belongsToMany('Torneo','torneos_usuarios')->withPivot('equipo', 'puntuacion');
	}
	
	public function hasRole($nombreRol)
	{
		$roles=array(
				'redactor'=>4,
				'admin'=>1,
				'usuario'=>'2',
				'admin.torneos'=>3
		);
					
		return in_array($roles[$nombreRol], array_fetch($this->roles->toArray(), 'id'));
	}
	
}